﻿namespace AllBots.ViewModel
{
    public class ViewModelLocator
    {
        private static ChatViewModel _chat;
        private static FamousBotsViewModel _famousBots;
        private static SearchIdViewModel searchId;
        private static WelcomeViewModel welcome;
        private static LibraryViewModel library;

        public ViewModelLocator()
        {
            CreateChat();
            CreateFamousBots();
        }

        public static ChatViewModel ChatStatic
        {
            get
            {
                if (_chat == null)
                {
                    CreateChat();
                }

                return _chat;
            }
        }
        public static FamousBotsViewModel FamousBotsStatic
        {
            get
            {
                if (_famousBots == null)
                {
                    CreateFamousBots();
                }

                return _famousBots;
            }
        }
        public static SearchIdViewModel SearchIdStatic
        {
            get
            {
                if (searchId == null)
                {
                    CreateSearchId();
                }

                return searchId;
            }
        }
        public static WelcomeViewModel WelcomeStatic
        {
            get
            {
                if (welcome == null)
                {
                    CreateWelcome();
                }

                return welcome;
            }
        }
        public static LibraryViewModel LibraryStatic
        {
            get
            {
                if (library == null)
                {
                    CreateLibrary();
                }

                return library;
            }
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
            "CA1822:MarkMembersAsStatic",
            Justification = "This non-static member is needed for data binding purposes.")]
        public ChatViewModel Chat
        {
            get
            {
                return ChatStatic;
            }
        }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
            "CA1822:MarkMembersAsStatic",
            Justification = "This non-static member is needed for data binding purposes.")]
        public SearchIdViewModel SearchId
        {
            get
            {
                return SearchIdStatic;
            }
        }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
            "CA1822:MarkMembersAsStatic",
            Justification = "This non-static member is needed for data binding purposes.")]
        public FamousBotsViewModel FamousBots
        {
            get { return FamousBotsStatic; }
        }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
            "CA1822:MarkMembersAsStatic",
            Justification = "This non-static member is needed for data binding purposes.")]
        public WelcomeViewModel Welcome
        {
            get
            {
                return WelcomeStatic;
            }
        }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
            "CA1822:MarkMembersAsStatic",
            Justification = "This non-static member is needed for data binding purposes.")]
        public LibraryViewModel Library
        {
            get
            {
                return LibraryStatic;
            }
        }

        public static void ClearChat()
        {
            _chat.Cleanup();
            _chat = null;
        }
        public static void ClearFamousBots()
        {
            _famousBots.Cleanup();
            _famousBots = null;
        }
        public static void ClearSearchId()
        {
            searchId.Cleanup();
            searchId = null;
        }
        public static void ClearWelcome()
        {
            welcome.Cleanup();
            welcome = null;
        }
        public static void ClearLibrary()
        {
            library.Cleanup();
            library = null;
        }

        private static void CreateChat()
        {
            if (_chat == null)
            {
                _chat = new ChatViewModel();
            }
        }
        private static void CreateFamousBots()
        {
            if (_famousBots == null)
            {
                _famousBots = new FamousBotsViewModel();
            }
        }
        private static void CreateSearchId()
        {
            if (searchId == null)
            {
                searchId = new SearchIdViewModel();
            }
        }
        private static void CreateWelcome()
        {
            if (welcome == null)
            {
                welcome = new WelcomeViewModel();
            }
        }
        private static void CreateLibrary()
        {
            if (library == null)
            {
                library = new LibraryViewModel();
            }
        }

        public static void Cleanup()
        {
            ClearChat();
            ClearFamousBots();
            ClearSearchId();
            ClearWelcome();
            ClearLibrary();
        }
    }
}